var user = getUserInfo();

var ShareUtil = {
    openShareUrl: function (url) {
        var a = document.createElement("a");
        a.href = url;
        console.log(url);
        a.target = "_blank";
        a.click();
    },
    shareToQZone: function (option) {
        ajaxGet("/strategies/strategyShare", {sid: vue.strategy.id}, function (data) {
            if (data.data) {
                var url = "https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey" +
                    "?url=" + encodeURIComponent(option.url) +
                    "&title=" + vue.strategy.title +
                    "&summary=" + vue.strategy.summary +
                    "&pics=" + vue.strategy.coverUrl;
                ShareUtil.openShareUrl(url);
            }else{
                popup("您已经分享过该攻略");
            }
            window.location.reload();
        });
        vue.queryStatisVo(vue.strategy.id);
    }
};

var vue = new Vue({
    el:"#app",
    data:{
        strategy:{},
        page:{},
        vo:{},
        sids:[]
    },
    methods:{
        strategyThumbup:function(){
            ajaxPost("/strategies/strategyThumbup",{sid:vue.strategy.id}, function (data) {
                if(data.data){
                    popup("顶成功啦");
                }else{
                    popup("今天你已经定过了");
                }
                vue.queryStatisVo(vue.strategy.id);
            })
        },
        favor:function(){
            ajaxPost("/strategies/favor",{sid:vue.strategy.id}, function (data) {
                if(data.data){
                    popup("收藏成功");
                }else{
                    popup("已取消收藏");
                }
                vue.queryStatisVo(vue.strategy.id);
                if(user){
                    vue.queryUserFavor(vue.strategy.id,user.id);
                }

            })
        },
        contentFocus:function(){
            $("#content").focus();
        },
        commentThumb:function(commentId){
            var page = $("#pagination").find("a.active").html()||1;
            ajaxPost("/strategies/commentThumb",{cid:commentId,sid:getParams().id}, function (data) {
                vue.commentPage(page,getParams().id);
            })
        },
        mouseover:function(even){
            $(even.currentTarget).find(".rep-del").css("display", "block");
        },
        mouseout:function(even){
            $(even.currentTarget).find(".rep-del").css("display", "none");
        },
        commentPage:function (page,strategyId) {//分页
            strategyId = strategyId || vue.strategy.id;
            ajaxGet("/strategies/comments", {currentPage:page, strategyId:strategyId}, function(data){
                vue.page = data.data;
                buildPage(vue.page.number, vue.page.totalPages,vue.commentPage);
            })
        },
        addComment:function(){ //添加评论
            var param = {}
            param.strategyId = vue.strategy.id;
            param.strategyTitle = vue.strategy.title;

            var content = $("#content").val();
            if(!content){
                popup("评论内容必填");
                return;
            }
            param.content = content;
            $("#content").val('');

            ajaxPost("/strategies/addComment",param, function (data) {
                vue.queryStatisVo(param.strategyId);
                vue.commentPage(1,param.strategyId);
            })
        },
        queryStatisVo:function (sid) {
            //统计数据
            ajaxGet("/strategies/statisVo",{sid:sid}, function (data) {
                vue.vo =data.data;
            })

        },
        queryUserFavor:function (sid,userId) {
            ajaxGet("/users/strategies/favor",{sid:sid, userId:userId}, function (data) {
                vue.sids = data.data;
            })
        },
        // shareToQzone:function () {
        //     var _title,_source,_sourceUrl,_pic,_showcount,_desc,_summary,_site,
        //         _url = 'http://www.newsucai.cn',
        //         _pic = 'http://www.newsucai.cn/static/home/images/jpg/__header.jpg';
        //     event.preventDefault();
        //     var _shareUrl = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?';
        //     _shareUrl += 'url=' + encodeURIComponent(_url||window.location.href);   //参数url设置分享的内容链接|默认当前页location
        //     _shareUrl += '&showcount=' + _showcount||0;      //参数showcount是否显示分享总数,显示：'1'，不显示：'0'，默认不显示
        //     _shareUrl += '&desc=' + encodeURIComponent(_desc||'这篇文章不错,分享一下~~');    //参数desc设置分享的描述，可选参数
        //     _shareUrl += '&summary=' + encodeURIComponent(_summary||vue.strategy.content);    //参数summary设置分享摘要，可选参数
        //     _shareUrl += '&title=' + encodeURIComponent(_title||vue.strategy.title);    //参数title设置分享标题，可选参数
        //     _shareUrl += '&site=' + encodeURIComponent(_site||window.location.href);   //参数site设置分享来源，可选参数
        //     _shareUrl += '&pics=' + encodeURIComponent(vue.strategy.coverUrl);   //参数pics设置分享图片的路径，多张图片以＂|＂隔开，可选参数
        //     window.open(_shareUrl, 'qZone', 'height=430, width=620, left=400, top=200');
        // },
        shareToWeibo:function () {
            alert("你妈炸了")
        }
    },

    filters:{
        dateFormat:function(date){
            return dateFormat(date, "YYYY-MM-DD HH:mm:ss")
        }
    },
    mounted:function () {
        var param = getParams();

        //攻略明细
        ajaxGet("/strategies/detail",{id:param.id}, function (data) {
            vue.strategy = data.data;


        })

        //统计数据
        this.queryStatisVo(param.id);



        //用户收藏攻略id集合
        if(user){
            this.queryUserFavor(param.id,user.id);
        }

        //攻略评论分页
        this.commentPage(1, param.id);
    }
});

